package zhishu.iceyyy.com.bxq_2500.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import zhishu.iceyyy.com.bxq_2500.R;
import zhishu.iceyyy.com.bxq_2500.api.API;
import zhishu.iceyyy.com.bxq_2500.api.Okhttp;
import zhishu.iceyyy.com.bxq_2500.base.ALL_CONFIG;
import zhishu.iceyyy.com.bxq_2500.bean.Type;
import zhishu.iceyyy.com.bxq_2500.bean.Version;
import zhishu.iceyyy.com.bxq_2500.utils.JSONUtils;
import zhishu.iceyyy.com.bxq_2500.utils.SpfUtils;


/**
 * 闪屏页面
 */
public class Splash_Activity extends FragmentActivity {


    private static final int GO_HOME = 1000;
    // 延迟3秒
    private static final long SPLASH_DELAY_MILLIS = 3000;
    /**
     * Handler:跳转到不同界面
     */
    public Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case GO_HOME:
                    if ((Boolean) SpfUtils.get(Splash_Activity.this, "islogin", false)) {
                        startActivity(new Intent(Splash_Activity.this, MainActivity.class));
                        finish();
                    } else {
                        startActivity(new Intent(Splash_Activity.this, MainActivity.class));
                        finish();
                    }

                    break;
            }
            super.handleMessage(msg);
        }
    };
    List<String> ccList = new ArrayList<String>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash_);
        Update();

        //延迟3秒

    }

    public void Update() {

        API.version(new Okhttp.Objectcallback() {
            @Override
            public void onsuccess(String st) {
                if (ALL_CONFIG.Debug) {
                    Log.e("version", st);
                }

                Version sign_bean = JSONUtils.parseJSON(st, Version.class);
                if (sign_bean.getCode().equals("200")) {
                    Version.DataBean data = sign_bean.getData();
                    Version.DataBean.VersionBean version1 = data.getVersion();

                }
                movieetype();
            }

            @Override
            public void onFalia(int code, String errst) {
                Log.e("711GetMobileCode", errst);
                movieetype();
            }
        });
    }

    public void movieetype() {
        API.Movicetype(new Okhttp.Objectcallback() {
            @Override
            public void onsuccess(String st) {
                Log.e("11111", "xx" + st);
                Type type = JSONUtils.parseJSON(st, Type.class);
                SpfUtils.put(Splash_Activity.this, "Movicetype", st);
                mHandler.sendEmptyMessageDelayed(GO_HOME, SPLASH_DELAY_MILLIS);
                if (type.getCode().equals("200")) {
                    List<Type.DataBean> dataBeans = type.getData();
                    ALL_CONFIG.dataBeans = dataBeans;
                    ccList.clear();
                    for (int i = 0; i < ccList.size(); i++) {
                        ccList.add(dataBeans.get(i).getType_name());
                    }
                    String[] array = ccList.toArray(new String[ccList.size()]);
                    ALL_CONFIG.array = array;

                } else {
                    Toast.makeText(Splash_Activity.this, type.getMessage(), Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFalia(int code, String errst) {


                String movicetype = (String) SpfUtils.get(Splash_Activity.this, "Movicetype", "");
                if (TextUtils.isEmpty(movicetype)) {
                    Toast.makeText(Splash_Activity.this, "请连接网络！", Toast.LENGTH_SHORT).show();
                } else {
                    Type type = JSONUtils.parseJSON(movicetype, Type.class);
                    if (type.getCode().equals("200")) {
                        List<Type.DataBean> dataBeans = type.getData();
                        ALL_CONFIG.dataBeans = dataBeans;
                        ccList.clear();
                        for (int i = 0; i < ccList.size(); i++) {
                            ccList.add(dataBeans.get(i).getType_name());
                        }
                        String[] array = ccList.toArray(new String[ccList.size()]);
                        ALL_CONFIG.array = array;

                    } else {
                        Toast.makeText(Splash_Activity.this, type.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                }
                mHandler.sendEmptyMessageDelayed(GO_HOME, SPLASH_DELAY_MILLIS);
            }

        });

    }

}
